import pyqtgraph as pg
import numpy as np
class PressureWidget(pg.GraphicsLayoutWidget):
    def __init__(self,title='压力曲线',datSize=500,freq=30):
        super().__init__()
        #   四个传感器的数值
        self.datA=[]
        self.datB = []
        self.datC = []
        self.datD = []
        self.datT = []

        self.datSize=datSize
        self.freq=freq
        self.stamps=np.linspace(0,datSize/freq,datSize)
        self.title=title
        self.InitDat()
    def InitDat(self):
        '''
        初始化
        :return:
        '''
        for i in range(self.datSize):
            self.datA.append(0.0)
            self.datB.append(0.0)
            self.datC.append(0.0)
            self.datD.append(0.0)
            self.datT.append(0.0)
        self.canvas=self.addPlot(title=self.title)
        self.canvas.setLabel('left','压力(N)')
        self.canvas.addLegend()

        self.cA=self.canvas.plot(pen="r",name="前左")
        self.cA.setData(self.stamps, self.datA)
        self.cB = self.canvas.plot(pen="g", name="前右")
        self.cB.setData(self.stamps, self.datB)
        self.cC = self.canvas.plot(pen="b", name="后左")
        self.cC.setData(self.stamps, self.datC)
        self.cD = self.canvas.plot(pen="y", name="后右")
        self.cD.setData(self.stamps, self.datD)
        self.cT = self.canvas.plot(pen="#fff", name="总和")
        self.cT.setData(self.stamps, self.datD)

    def UpdateData(self,p):
        '''
        更新数据
        :param l:
        :param r:
        :param b:
        :return:
        '''
        self.datA.append(p[0]);
        self.datA.pop(0)
        self.datB.append(p[1]);
        self.datB.pop(0)
        self.datC.append(p[2]);
        self.datC.pop(0)
        self.datD.append(p[3]);
        self.datD.pop(0)
        #   计算总和
        self.datT.append(p[0]+p[1]+p[2]+p[3])
        self.datT.pop(0)
        self.cA.setData(self.stamps, self.datA)
        self.cB.setData(self.stamps, self.datB)
        self.cC.setData(self.stamps, self.datC)
        self.cD.setData(self.stamps, self.datD)
        self.cT.setData(self.stamps, self.datT)

if __name__=='__main__':
    f=PressureWidget()
    pg.exec()


